<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar  @input="input" cancelButton="none" radius="100" :focus="true" @confirm="search" ></uni-search-bar>
    </view>
    
    <!-- 搜索结果列表 -->
    <view class="search-list" v-if="searchList.length!==0">
      <view class="search-item" v-for="(item,index) in searchList" :key="index" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
        <view class="history-title">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
        </view>
        <view class="history-list" >
          <uni-tag class="history-item" v-for="(item,index) in historys" :key="index" :circle="true" :text="item" inverted="true" @click="gotoGoodsList(item)"></uni-tag> 
        </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //延时器的ID
        timerID:null,
        //输入框内不断输入的值
        keyWords:'',
        //输入框联想的搜索列表
        searchList:[],
        //历史搜索记录
        searchHistoryList:[],
        //搜索的最近一次内容
        searchKeyValue:''
      };
    },
    onLoad() {
   this.searchHistoryList=  JSON.parse( uni.getStorageSync('ky') || '[]')
    },
    methods:{
      search(e){
        console.log(e,5023)
        this.searchKeyValue = e.value
        //存储历史记录
        this.saveSearchHistory()
      },
      input(value){
        clearTimeout(this.timerID)
        this.timerID = setTimeout(()=>{
           this.keyWords = value
           //获取搜索列表
           this.getSearchList()

          console.log(this.keyWords)
        },500)
      },
      //获取搜索列表
     async getSearchList(){
        //判断搜索关键词是否为空
          if(this.keyWords.length === 0){
            this.searchList=[]
            return
          }
          const res = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.keyWords})
          if(res.data.meta.status !== 200) return uni.$showMsg
          this.searchList = res.data.message
      },
      //点击搜索结果跳转商品详情页
      gotoDetail(goods_id){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+goods_id
        })
      },
      //存储历史记录
      saveSearchHistory(){
        // this.searchList.push(this.keyWords)
        const set = new Set(this.searchHistoryList)
        set.delete(this.searchKeyValue)
        set.add(this.searchKeyValue)
        this.searchHistoryList = Array.from(set)
        uni.setStorageSync('ky',JSON.stringify(this.searchHistoryList))
      } ,
      cleanHistory(){
        this.searchHistoryList = []
        uni.setStorageSync('ky','[]')
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + item
        })
      }
      },
      computed: {
        historys() {
          return this.searchHistoryList.reverse()
        }
      }
    }
  
</script>

<style lang="scss">
.search-box {
position: sticky;
top: 0;
z-index: 999;
}

.search-list{
  padding: 0 5px;
  .search-item{
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 13px 0;
    border-bottom: 1px solid #efefef;
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}

.history-box{
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
